package com.vast.admin.api.wechatpay;

import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.Base64;

@Slf4j
@Component
public class ParseReqInfo {

    /**
     * 解码器
     */
    private static Cipher cipher = null;

    @Autowired
    private ZfwWXPayConfig zfwWXPayConfig = new ZfwWXPayConfig();

    public ParseReqInfo() {
        init();
    }

    public static void main(String[] args) {
        ParseReqInfo parseReqInfo = new ParseReqInfo();
        String response = "WMXGjGwO0YHjhkFDKoSlwJWJ44xhsVonYRvTSmIbNJsa2UvW8YYLKFqsh6Ecibt/oMnhgybmD7Ho1RfLHdjyY9/SMDrdxLwDz84F5DYZwH6iHBEc5Fogs3KFJk/29qccrL+iSDN5y/AQN0L8auDXpHo4piQJkMYVbfL3L6X0ydkIBwK7VMrwLM1Kg7k0FGfQzx/MwxONXtlJemWBCaSfs2n3PcE0VqxFyfPRjmkasg8XkD7wq3MnW3dEq7frXfan/j+KqrFrXr0zKJp7NvhtSgTW3fu/cE1WZZIHWGHz/XiRFB1SQA0/0tu6KaaxCI+QtGs3lD9yfaekOmOkJwE2dUB4HI7pSdN1ZSorPX1iMmPf1dHQgXUQZW/zP58WRmWs1RwrcsOZlhdjhIb+f6C5V9htR1wes5zGlVDb37QJ01E20tF/1MtEI3nCskauJ1GR58tgEHOLDl74pmnD6dZ/BMnOYkocFzufTkaPMxezu74WDWP6koFO0GMXXRV1CD6F6IEtu5bJHQY+sUv/s1td+R9NXiD7zDaKn6D2EM5tljypnpM98kZI1EgVEXCfctCVw0amnN307d39eD06ZZ0dIqMxl2m7woTsRtH7pdDVuE2XKXc0eNrZdcPnii7iXTvVh1yM+wC+AeqT3xPB1jz+2qENJABpPw9afd2zNd+BJ+FA0CN5NYX6QZn1HCH5ExJsjtpAinH84yuX8bfD9FW8XFaOU4Rk3bnq9ibcSc7/D53dIVvB964KzvlznjObs0hqj9t3oI3eiQeudeqjnv42O/Q3Rvg+l0fk1k29FwsFSRNdM96rT5rOq1sRdDuWo1kM1lVSzjSWh5ZugWi6j/JBVwIFga7ZIqh+JpkxnLmirwqAmvqg6JxVu53vRm/+OKg1LagB9lqzr4plsdXdbklNuoHUE9igprJiPrnaK6T/awCZvAWBnXVDm8c66NkwTxBlI31dOEF6iqCC9JEUMrIdsfeT3JSI0uHdyLHRuDLTPpmdonXreYCIPUhfZuUDF7yzqdn1oB8fA8NraH7M3NDHtMAUcQM/YNCGTpDAUdE6CmtUdq7LcbwQgGt2v0zxWosx";
        try {
            parseReqInfo.parseReqInfo(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String parseReqInfo(String reqInfo) throws Exception {
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] base64ByteArr = decoder.decode(reqInfo);
        String result = new String(cipher.doFinal(base64ByteArr));
        log.info("解密结果:{}" + result);
        return result;
    }

    public void init() {
        String key = getMD5(zfwWXPayConfig.getKey());
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
        Security.addProvider(new BouncyCastleProvider());
        try {
            cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
    }

    public static String getMD5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            String result = MD5(str, md);
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String MD5(String strSrc, MessageDigest md) {
        byte[] bt = strSrc.getBytes();
        md.update(bt);
        String strDes = bytes2Hex(md.digest());
        return strDes;
    }

    public static String bytes2Hex(byte[] bts) {
        StringBuffer des = new StringBuffer();
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des.append("0");
            }
            des.append(tmp);
        }
        return des.toString();
    }

}
